'use strict';

var permissionImpl = require('./dao_impl/permission.js');
var roleImpl = require('./dao_impl/role.js');
var ferror = require('../common/app_utils.js').formatError;
var fout = require('../common/app_utils.js').formatOutput;
var errInfo = require('../common/err_info.js');
var debug = require('debug')('dao_role');

function MongoDao() {
	var self = this instanceof MongoDao ? this : new MongoDao();
	return self;
}

module.exports = new MongoDao();
var theProto = MongoDao.prototype;

/*
{	
"name":"role_vendor",
"description":"商家",
"permissions":[123,456]
}
*/
theProto.create = function(json) {
	debug('create role', json);
	return this.checkPermissionEntryExist(json.permissions)

	.then(function(result) {
		if (result.errCode !== 0) {
			return result;
		}
		return roleImpl.create(json);
	});
};

theProto.checkPermissionEntryExist = function(permissions) {
	debug('checkPermissionEntryExist', permissions);
	if (!permissions || permissions.length === 0) {
		return new Promise(function(f, r) {
			f(fout('empty input'));
		});
	}

	var query = {
		code: {
			$in: permissions
		}
	};
	debug('query=', query);

	return permissionImpl.read({
		query: query
	})

	.then(function(result) {
		debug('check permissions exist, result=', JSON.stringify(result));
		if (result.errCode !== 0) {
			return result;
		}

		// debug('two length', result.result.length, json.permissions.length);
		// some permission not exists
		if (result.result.length < permissions.length) {
			var found = result.result.map(function(p) {
				return p.name;
			});
			var notFount;
			for (var queryName of permissions) {
				if (found.indexOf(queryName) < 0) {
					notFount = queryName;
					break;
				}
			}
			return ferror(errInfo.E2201, "permission not exists: " + notFount);
		}
		return result;
	});
};

/*
{
"_id":"xxid",
"name":"role_vendor",
"description":"商家",
"permissions":[123,456]
}
*/
theProto.update = function(json) {
	debug('update role', json);
	// TODO check _id, permissions 
	return this.checkPermissionEntryExist(json.permissions)

	.then(function(result) {
		if (result.errCode !== 0) {
			return result;
		}
		return roleImpl.findByIdAndUpdate(json._id, {
			name: json.name,
			description: json.description,
			permissions: json.permissions || []
		}, {
			new: true
		});
	});
};


// userId
theProto.readAll = function(json) {

	// return acl(json.userId, permissions.role_read, null)

	// .then(function(result) {
	// 	if (result.errCode !== 0) {
	// 		return ferror(errInfo.E2203);
	// 	}
	// 	// have permission
	// 	var obj = {
	// 		query: {}
	// 	};
	// 	return permissionImpl.read(json);
	// });

	// have permission
	var obj = {
		query: {}
	};
	return roleImpl.read(json);
};

// userId
theProto.readById = function(json) {
	var id = json.id;
	// errCode:0, result:{role:xxObj, permissions:xxxObj}
	var ret = {};
	return roleImpl.readById(id)

	.then(function(result) {
		ret.role = result.result;
		return permissionImpl.read({
			query: {}
		});
	}).then(function(result) {
		ret.permissions = result.result;
		return fout(ret);
	});
};